package com.unity.bh3.controller;

import com.unity.bh3.common.Result;
import com.unity.bh3.dto.ResourceChangeRequest;
import com.unity.bh3.dto.UserInfoResponse;
import com.unity.bh3.dto.UserLoginRequest;
import com.unity.bh3.dto.UserRegisterRequest;
import com.unity.bh3.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 */
@Tag(name = "用户管理", description = "用户相关接口，包括注册、登录、获取用户信息等")
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户注册
     */
    @Operation(summary = "用户注册", description = "注册新用户，返回用户信息")
    @PostMapping("/register")
    public Result<UserInfoResponse> register(@Valid @RequestBody UserRegisterRequest request) {
        UserInfoResponse response = userService.register(request);
        return Result.success(response);
    }

    /**
     * 用户登录
     */
    @Operation(summary = "用户登录", description = "用户登录，返回用户信息")
    @PostMapping("/login")
    public Result<UserInfoResponse> login(@Valid @RequestBody UserLoginRequest request) {
        UserInfoResponse response = userService.login(request);
        return Result.success(response);
    }

    /**
     * 获取用户信息
     */
    @Operation(summary = "获取用户信息", description = "根据用户ID获取用户信息")
    @Parameters({
            @Parameter(name = "User-Id", ref = "User-Id")
    })
    @GetMapping("/{userId}")
    public Result<UserInfoResponse> getUserInfo(
            @Parameter(description = "用户ID", required = true) 
            @PathVariable Long userId) {
        UserInfoResponse response = userService.getUserInfo(userId);
        return Result.success(response);
    }

    /**
     * 增加钻石
     */
    @Operation(summary = "增加钻石", description = "为指定用户增加钻石数量")
    @Parameters({
            @Parameter(name = "User-Id", ref = "User-Id")
    })
    @PostMapping("/diamond/add")
    public Result<Boolean> addDiamond(@Valid @RequestBody ResourceChangeRequest request) {
        boolean success = userService.addDiamond(request.getUserId(), request.getAmount());
        return Result.success(success);
    }

    /**
     * 减少钻石
     */
    @Operation(summary = "减少钻石", description = "为指定用户减少钻石数量")
    @Parameters({
            @Parameter(name = "User-Id", ref = "User-Id")
    })
    @PostMapping("/diamond/reduce")
    public Result<Boolean> reduceDiamond(@Valid @RequestBody ResourceChangeRequest request) {
        boolean success = userService.reduceDiamond(request.getUserId(), request.getAmount());
        return Result.success(success);
    }

    /**
     * 增加金币
     */
    @Operation(summary = "增加金币", description = "为指定用户增加金币数量")
    @Parameters({
            @Parameter(name = "User-Id", ref = "User-Id")
    })
    @PostMapping("/gold/add")
    public Result<Boolean> addGold(@Valid @RequestBody ResourceChangeRequest request) {
        boolean success = userService.addGold(request.getUserId(), request.getAmount());
        return Result.success(success);
    }

    /**
     * 减少金币
     */
    @Operation(summary = "减少金币", description = "为指定用户减少金币数量")
    @Parameters({
            @Parameter(name = "User-Id", ref = "User-Id")
    })
    @PostMapping("/gold/reduce")
    public Result<Boolean> reduceGold(@Valid @RequestBody ResourceChangeRequest request) {
        boolean success = userService.reduceGold(request.getUserId(), request.getAmount());
        return Result.success(success);
    }

    /**
     * 增加体力
     */
    @Operation(summary = "增加体力", description = "为指定用户增加体力值")
    @Parameters({
            @Parameter(name = "User-Id", ref = "User-Id")
    })
    @PostMapping("/stamina/add")
    public Result<Boolean> addStamina(@Valid @RequestBody ResourceChangeRequest request) {
        boolean success = userService.addStamina(request.getUserId(), request.getAmount());
        return Result.success(success);
    }

    /**
     * 减少体力
     */
    @Operation(summary = "减少体力", description = "为指定用户减少体力值")
    @Parameters({
            @Parameter(name = "User-Id", ref = "User-Id")
    })
    @PostMapping("/stamina/reduce")
    public Result<Boolean> reduceStamina(@Valid @RequestBody ResourceChangeRequest request) {
        boolean success = userService.reduceStamina(request.getUserId(), request.getAmount());
        return Result.success(success);
    }
}